<?php

namespace Eduxplus\CmsBundle\Controller\Admin;

use Eduxplus\CoreBundle\Lib\Base\BaseAdminController;
use Eduxplus\CoreBundle\Lib\Form\Form;
use Eduxplus\CmsBundle\Service\Admin\TutorialChapterService;
use Eduxplus\CmsBundle\Service\Admin\TutorialService;
use Symfony\Component\HttpFoundation\Request;
use Eduxplus\CoreBundle\Service\DraftService;

class TutorialChapterController extends BaseAdminController
{

    const DRAFT_TYPE = 301;

    public function indexAction($id, TutorialChapterService $chapterService)
    {
        $data = [];
        $data['all'] = $chapterService->getChapterTree(0, $id);
        $data['id'] = $id;
        return $this->render("@CmsBundleAdmin/tutorial/chapter/index.html.twig", $data);
    }


    public function addAction($id, Form $form, Request $request, TutorialChapterService $chapterService, TutorialService $tutorialService)
    {
        $select = $chapterService->chapterSelect($id);

        $parentId = $request->get("pid");
        $form->text("名称")->field("name")->isRequire(1);
        $form->select("父章节")->field("parentId")->isRequire(1)->defaultValue($parentId)->options($select);
        $form->text("标签")->field("tags")->isRequire(1)->placeholder("多个标签以英文,隔开");
        $form->boole("上架？")->field("status")->isRequire(1);
        $form->text("排序")->field("sort")->isRequire(1)->defaultValue(0);

        $formData = $form->create($this->generateUrl("admin_api_cms_tutorial_chapter_add", [
            'id' => $id
        ]));
        return $this->content()->title("添加章节")
            ->breadcrumb("教程管理", "admin_cms_tutorial_index")
            ->breadcrumb("章节管理", "admin_cms_tutorial_chapter_index", ["id"=>$id])
            ->renderAdd($formData);
    }


    public function addDoAction($id, Request $request, TutorialChapterService $chapterService)
    {
        $name = $request->get("name");
        $parentId = (int) $request->get("parentId");
        $status = $request->get("status");
        $sort = (int) $request->get("sort");
        $tags = $request->get("tags");

        $status = $status == "on" ? 1 : 0;

        if (!$name) return $this->responseError("章节名称不能为空!");
        if (mb_strlen($name, 'utf-8') > 50) return $this->responseError("章节名称不能大于50字!");
        if(!$tags) return $this->responseError("标签不能为空!");

        $chapterService->add($name, $parentId,$sort, $id, $status,$tags);

        return $this->responseMsgReload("操作成功!");
    }

    public function editContentAction($id, Form $form, TutorialChapterService $chapterService)
    {
        $info = $chapterService->getById($id);
        $form->richEditor("内容")->field("content")->attr(["height"=>600])->isRequire(1)->defaultValue($info["content"]);
        $formData = $form->create($this->generateUrl("admin_api_cms_tutorial_chapter_edit_content", [
            'id' => $id
        ]));
        return $this->content()->title("编辑内容")->renderView($formData);
    }


    public function editContentDoAction($id, Request $request, TutorialChapterService $chapterService)
    {
        $content = $request->get("content");
        if (!$content) return $this->responseError("内容不能为空!");
        $chapterService->editContent($id, $content);

        return $this->responseMsgSuccess("操作成功!");
    }


    public function editAction($id, Form $form, TutorialChapterService $chapterService, TutorialService $tutorialService)
    {
        $info = $chapterService->getById($id);

        $select = $chapterService->chapterSelect($info["tutorialId"]);

        $form->text("名称")->field("name")->isRequire(1)->defaultValue($info['name']);
        $form->select("父章节")->field("parentId")->isRequire(1)->defaultValue($info['parentId'])->options($select);
        $form->text("标签")->field("tags")->isRequire(1)->placeholder("多个标签以英文,隔开")->defaultValue($info['tags']);
        $form->boole("上架？")->field("status")->isRequire(1)->defaultValue($info['status']);
        $form->text("排序")->field("sort")->isRequire(1)->defaultValue(0)->defaultValue($info['sort']);

        $formData = $form->create($this->generateUrl("admin_api_cms_tutorial_chapter_edit", [
            'id' => $id
        ]));
        return $this->content()->title("编辑章节")->renderView($formData);
    }


    public function editDoAction($id, Request $request, TutorialChapterService $chapterService)
    {
        $name = $request->get("name");
        $parentId = (int) $request->get("parentId");
        $status = $request->get("status");
        $sort = (int) $request->get("sort");
        $status = $status == "on" ? 1 : 0;
        $tags = $request->get("tags");

        if (!$name) return $this->responseError("章节名称不能为空!");
        if (mb_strlen($name, 'utf-8') > 50) return $this->responseError("章节名称不能大于50字!");
        if (!$tags) return $this->responseError("标签不能为空!");

        $chapterService->edit($id, $name, $parentId, $sort, $status, $tags);

        return $this->responseMsgSuccess("操作成功!");
    }


    public function deleteDoAction($id, TutorialChapterService $chapterService)
    {
        if ($chapterService->hasChild($id)){
            return $this->responseError("删除失败，请先删除子章节!");
        }
        $chapterService->del($id);

        return $this->responseMsgReload("删除成功!");
    }


    public function updateSortDoAction($id, Request $request, TutorialChapterService $chapterService)
    {
        $data = $request->request->all();
        $chapterService->updateSort($data);
        return $this->responseMsgRedirect("更新排序成功!", $this->generateUrl("admin_cms_tutorial_chapter_index", [
            "id" => $id
        ]));
    }
}
